layui.use(["jquery", "table"], function () {
  var table = layui.table, //表格
    roleId,
    tableArray = [],
    $ = layui.jquery;
  // 班次管理列表 roleManagementSup
  //监听行工具事件
  tableOn(
    table,
    "roleManagementSup",
    function () {
      layer.msg("编辑操作");
    },
    function (obj, index) { }
  );
  var roleManagementSup = tableRender(table, {
    elem: "#roleManagementSup",
    url: "/ap/selectByParamList.json",
    height: 300,
    contentType: "",
    cols: [
      [
        //表头
        {
          title: "序号",
          type: "numbers"
        },
        {
          width: 165,
          align: "center",
          toolbar: "#onlyEdit",
          title: "操作",
        },
        {
          field: "tcRoleName",
          title: "角色名称",
        },
        {
          field: "tcRoleDsc",
          title: "角色描述",
        }
      ]
    ],
    done(res) {
      roleId = res.data[0].tcRoleId;
      getRoleManagementSub(res.data[0].tcRoleId);
    }
  });

  // 班次管理列表 roleManagementSub
  //监听行工具事件
  tableOn(
    table,
    "roleManagementSub",
    function () {
    },
    function (obj, index) {
      layer.closeAll();
      layer.msg("正在操作...", { shade: 0.3, time: 200000000 });
      comment(
        "/ap/delUserRole.json",
        "post",
        {
          roleId,
          gen01: [obj.data.gen01]
        },
        function (res) {
          if (res.code == "0") {
            reload(roleManagementSub, {
              roleId
            });
            layer.msg("操作成功", { time: 1000 });
          } else {
            layer.msg("操作失败", { time: 1000 });
          }
        },
        true
      );
    }
  );

  table.on("checkbox(roleManagementSub)", function (obj) {
    // checkbox 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
    tableArray = table.checkStatus("roleManagementSub").data; // 获取勾选数据
  });

  // 批量删除
  $(".delete").on("click", function () {
    if (tableArray.length == 0) return layer.msg("请选择一条数据进行操作");
    var str = [];
    for (var i = 0; i < tableArray.length; i++) {
      str.push(tableArray[i].gen01);
    }
    layer.confirm("确定删除吗", function () {
      layer.closeAll();
      layer.msg("正在操作...", { shade: 0.3, time: 200000000 });
      comment(
        "/ap/delUserRole.json",
        "post",
        {
          roleId,
          gen01: str
        },
        function (res) {
          if (res.code == "0") {
            reload(roleManagementSub, {
              roleId
            });
            layer.msg("操作成功", { time: 1000 });
          } else {
            layer.msg("操作失败", { time: 1000 });
          }
        },
        true
      );
    });
  });

  function getRoleManagementSub(roleId) {
    roleManagementSub = tableRender(table, {
      elem: "#roleManagementSub",
      url: "/ap/selectUserByRoleId.json",
      height: 300,
      contentType: "",
      where: { roleId },
      cols: [
        [
          //表头
          {
            type: "checkbox"
          },
          {
            title: "序号",
            type: "numbers"
          },
          {
            // width: 165,
            align: "center",
            toolbar: "#onlyDel",
            title: "操作",
          },
          {
            field: "gen01",
            title: "工号",
          },
          {
            field: "gen02",
            title: "姓名",
          },
          {
            field: "gem02",
            title: "部门",
          },
          {
            field: "tcHrJob",
            title: "岗位",
          }
        ]
      ]
    });
  }
});
